package com.qiwenshare.file.task;

import com.qiwenshare.file.domain.SystemDO;
import com.qiwenshare.file.service.SystemService;
import com.qiwenshare.file.util.NetWorkUtil;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class MyScheduledService {

    @Autowired
    SystemService systemService;


    @Scheduled(fixedRate = 1000) // 每秒刷新一次
    public void refreshSystemInfo() {
        // 获取CPU占用率
        double cpuUsage = getCpuUsage()/100;
        //log.info("cpu使用率: " + cpuUsage + "%");

        // 获取内存信息
        double totalMemory = getTotalMemory()/1024/1024;
        double usedMemory = getUsedMemory()/1024/1024;
        double memoryUsage = (usedMemory / totalMemory) ;
        //log.info("总内存: " + totalMemory + " MB");
        //log.info("已使用内存: " + usedMemory + " MB");
        //log.info("内存占用率: " + memoryUsage + "%");

        // 获取磁盘信息
        double totalDiskSpace = getTotalDiskSpace()/1024/1024/1024;
        double usedDiskSpace = totalDiskSpace - getFreeDiskSpace()/1024/1024/1024;
        double diskUsage = (usedDiskSpace / totalDiskSpace) ;
        //log.info("总硬盘大小: " + totalDiskSpace + " GB");
        //log.info("已使用硬盘大小: " + usedDiskSpace + " GB");
        //log.info("硬盘使用率: " + diskUsage + "%");

        // 获取网络宽带流速
        Map<String, String> networkDownUp = NetWorkUtil.getNetworkDownUp();
        //上传速率
        Double rxPercent = Double.valueOf(networkDownUp.get("rxPercent"));
        //下载速率
        Double txPercent = Double.valueOf(networkDownUp.get("txPercent"));
        //log.info("上传速率: " + rxPercent + " kb");
        //log.info("下载速率: " + txPercent + " kb");

        LocalDateTime now = LocalDateTime.now();
        List<SystemDO> saveList = List.of(new SystemDO(null, 1, null, null, cpuUsage, now),
                new SystemDO(null, 2, usedMemory, totalMemory, memoryUsage, now),
                new SystemDO(null, 3, usedDiskSpace, totalDiskSpace, diskUsage, now),
                new SystemDO(null, 4, rxPercent, txPercent, null, now));

        log.info(saveList.toString());
        systemService.saveBatch(saveList);


    }

    private double getCpuUsage() {
        try {
            ProcessBuilder processBuilder = new ProcessBuilder("WMIC", "CPU", "GET", "LoadPercentage");
            Process process = processBuilder.start();

            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            int row = 1;
            while ((line = reader.readLine()) != null) {
                if(row == 3 ){
                    return Double.parseDouble(line.trim());
                }
                row++;
            }

            process.waitFor();
            process.destroy();

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        return 0.0;
    }

    private double getTotalMemory() {
        return Runtime.getRuntime().totalMemory();
    }

    private double getUsedMemory() {
        return getTotalMemory() - Runtime.getRuntime().freeMemory();
    }

    private double getTotalDiskSpace() {
        return new java.io.File("/").getTotalSpace();
    }

    private double getFreeDiskSpace() {
        return new java.io.File("/").getFreeSpace();
    }






}